package com.example.petgrowing;

import androidx.appcompat.app.AppCompatActivity;


import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class activity_register extends AppCompatActivity {
    public String TAG = "user-register";
    public EditText register_userAccount;
    public EditText register_userName;
    public EditText register_userPassword1;
    public EditText register_userPassword2;
    public Button btn_register;
    public DBHelper dbHelper;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_userAccount = findViewById(R.id.register_userAccount);
        register_userName = findViewById(R.id.register_userName);
        register_userPassword1 = findViewById(R.id.register_userPassword1);
        register_userPassword2 = findViewById(R.id.register_userPassword2);
        btn_register = findViewById(R.id.btn_register);
        dbHelper = new DBHelper(activity_register.this);

        btn_register.setOnClickListener(view -> {
            String edt_account = register_userAccount.getText().toString();
            String edt_name = register_userName.getText().toString();
            String edt_ps1 = register_userPassword1.getText().toString();
            String edt_ps2 = register_userPassword2.getText().toString();

            if(TextUtils.isEmpty(edt_account)){
                Toast.makeText(activity_register.this,"账号不能为空,请输入账号",Toast.LENGTH_SHORT).show();
            }
            else if(TextUtils.isEmpty(edt_name)){
                Toast.makeText(activity_register.this,"用户名不能为空,请输入用户名",Toast.LENGTH_SHORT).show();
            }
            else if(TextUtils.isEmpty(edt_ps1)||TextUtils.isEmpty(edt_ps2)){
                Toast.makeText(activity_register.this,"密码不能为空,请输入密码",Toast.LENGTH_SHORT).show();
            }
            else if (!edt_ps1.equals(edt_ps2)) {
                Toast.makeText(activity_register.this,"两次账号密码不一致，请重新设置密码",Toast.LENGTH_SHORT).show();
                register_userPassword1.setText("");
                register_userPassword2.setText("");
            }
            else{
                insertNewUser(dbHelper.getReadableDatabase(),edt_account,edt_name,edt_ps1);
                Toast.makeText(activity_register.this,"注册成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
            }
        });
    }
    public void insertNewUser(SQLiteDatabase readableDatabase, String useraccount, String username, String userpassword){
        ContentValues values = new ContentValues();
        values.put("userAccount",useraccount);
        values.put("userName",username);
        values.put("userPassword",userpassword);
        readableDatabase.insert("user",null,values);
    }
}
